#!/bin/bash

# color ->> default bootup look
# other ->> default bootup look without ANSI colors or positioning
BOOTUP=color
# Column to start "[  OK  ]" label in:
RES_COL=60
# terminal sequence to move to that column:
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
# Terminal sequence to set color to a 'success' (bright green):
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
# Terminal sequence to set color to a 'failure' (bright red):
SETCOLOR_FAILURE="echo -en \\033[1;31m"
# Terminal sequence to set color to a 'warning' (bright yellow):
SETCOLOR_WARNING="echo -en \\033[1;33m"
# Terminal sequence to reset to the default color:
SETCOLOR_NORMAL="echo -en \\033[0;39m"


# Define the success function
success () {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n " ["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo -n $" OK "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\\r"
  return 0
}

# Define the failure function
failure () {
  local rc=$?
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n " ["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo -n $"FAILED"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\\r"
  return $rc
}

# Define the action function
action () {
  local STRING=$1
  echo -n "$STRING "
  shift
  "$@" && success $"$STRING" || failure $"$STRING"
  local rc=$?
  echo
  return $rc
}

